<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的博客</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <link rel="stylesheet" href="css/list.css">

</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo.jpg" alt="">
        <span class="title">博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer" style="width: 25%;"></span>
        <div class="search">
            <input class="search-input" id="search-input" type="text" placeholder="输入文章标题">
            <button class="search-button" onclick="performSearch()">搜索</button>
        </div>
        <a href="blog_list.html">博客广场</a>
        <a href="personal_center.html">个人中心</a>
        <a href="my_relation_following.html">我的关注/粉丝</a>
        <a href="private_message.html">聊天室</a>
        <a href="draft_list.html">我的草稿</a>
        <a href="blog_add.html">写博客</a>
        <a href="javascript:logout()">注销</a>
    </div>

    <!-- 版心 -->
    <div class="container">
        <div class="container-left">
            <div class="card">
                <img id="avatar-img" src="img/avatar.png" class="avtar" alt="Avatar">
                <h3 id="author2"></h3>
                <!-- <button class="button attention-button"> + 关注</button> -->
                <!-- <button class="button attention-button"> 已关注</button> -->

                <button class="button my-attention-button" onclick="toFollowingPage()">我的关注</button>
                <button class="button my-fans-button" onclick="toFollowsPage()">我的粉丝</button>

                <a id="url" href="#" target="_blank">gitee 地址</a>
                <div class="counter">
                    <span class="icon icon-counter"></span>
                    <span id="counter">文章数量：0</span>
                </div>
                <div class="reading-times">
                    <span class="icon icon-reading"></span>
                    <span id="reading-times">总访问量：9999</span>
                </div>
                <div class="comment-times">
                    <span class="icon icon-comment"></span>
                    <span id="comment-times">获得评论：2 次</span>
                </div>

            </div>
        </div>

        <!-- 右侧内容详情 -->
        <div id="artlist" class="container-right">

            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <!-- <div class="blog">
                <div class="title">我的第一篇博客</div>
                <div class="date">2021-06-02</div>
                <div class="desc">
                    从今天起, 我要认真敲代码. Lorem ipsum, dolor sit amet consectetur adipisicing elit. Cum distinctio ullam eum ut
                    veroab laborum numquam tenetur est in dolorum a sint, assumenda adipisci similique quaerat vel.
                    Facere,
                    et.
                </div>
                <a href="blog_content.html?id=1" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;
                <a href="blog_edit.html?id=1" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;
                <a href="javascript:del(1)" class="detail">删除 &gt;&gt;</a>
            </div> -->
        </div>
    </div>

    <script src="js/jquery.min.js"></script>
    <script src="./js/logout.js"></script>
    <script src="./js/common.js"></script>
    <script>

        function getUserInfo() {
            jQuery.ajax({
                url: "user/getuserinfo",
                type: "POST",
                data: {},
                success: function (res) {
                    if (res.code == 200 && res.data != null && res.data.id > 0) {
                        // 获取用户信息成功！
                        // 头像
                        if (res.data.photo != null && res.data.photo != '') {
                            jQuery("#avatar-img").attr("src", "/photo/" + res.data.photo);
                        } else {
                            jQuery("#avatar-img").attr("src", "/img/avatar.png");
                        }

                        // 用户名或昵称
                        if (res.data.nickname != null && res.data.nickname != 'undefined') {
                            jQuery("#author2").html(res.data.nickname);
                        } else {
                            jQuery("#author2").html(res.data.username);
                        }

                        // gitee地址
                        if (res.data.gitee != null && res.data.gitee != '') {
                            jQuery("#url").attr("href", res.data.gitee);
                        } else {
                            console.log("该作者未设置gitee地址！");
                        }

                        // 获取总访问量和评论总数
                        getTotalRCountAndCommentByUId(res.data.id);

                    } else {
                        alert("获取作者信息失败！");
                        location.href = "blog_list.html";
                    }
                }
            });
        }

        getUserInfo();

        function getMyAtrList() {
            jQuery.ajax({
                url: "art/mylist",
                type: "POST",
                data: {},
                success: function (res) {
                    if (res.code == 200 && res.data.length > 0) {

                        // 文章数量
                        jQuery("#counter").html("文章总数：" + res.data.length);

                        // for 拼接 div，最终放到 mylist
                        var arthtml = "";
                        for (var i = 0; i < res.data.length; i++) {
                            var art = res.data[i];
                            arthtml += '<div class="blog">';
                            arthtml += '<div class="title">' + art.title + '</div>';
                            arthtml += '<div class="date">' + art.createtime + '</div>';
                            arthtml += '<div class="desc">';
                            arthtml += art.content;
                            arthtml += '</div>';
                            arthtml += '<a href="blog_content.html?id=' + art.id + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';
                            arthtml += '<a href="blog_edit.html?id=' + art.id + '" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;';
                            arthtml += '<a href="javascript:del(' + art.id + ')" class="detail">删除 &gt;&gt;</a>';
                            arthtml += '</div>';

                        }
                        jQuery("#artlist").html(arthtml);
                    } else {
                        // 暂无文章
                        jQuery("#artlist").html("<h2 style = 'margin-left:100px;margin-top:100px'>暂无文章！</h2>")
                    }
                }
            });
        }

        getMyAtrList();

        function del(id) {
            if (confirm("确认删除该博客？")) {
                jQuery.ajax({
                    url: "art/del",
                    type: "POST",
                    data: { "id": id },
                    success: function (res) {
                        if (res.code == 200 && res.data == 1) {
                            alert("删除成功！");
                            location.href = "myblog_list.html";
                        } else {
                            alert("操作失败，请重试！" + res.msg);
                        }
                    }
                });
            }
        }

        function performSearch() {
            var searchKey = jQuery("#search-input");
            if (searchKey.val().trim() == '') {
                alert("请先输入搜索关键字！");
                return;
            }

            console.log("搜索关键字：" + searchKey.val());
            var searchUrl = 'search_list.html?query=' + encodeURIComponent(searchKey.val());
            console.log("url：" + searchUrl);

            // 打开新窗口
            window.open(searchUrl, '_blank');
        }


        function toFollowingPage() {
            location.href = "my_relation_following.html";
        }

        function toFollowsPage() {
            location.href = "my_relation_follows.html";
        }

    </script>

</body>

</html>